<!--
  项目流程-中介选聘
  @author YangMiao 1939724848@qq.com
  @date 2022/3/30 15:42
  @version 1.0.0
-->
<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm" @submit.native.prevent>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('申请标题')" prop="actTitle">
            <el-input v-model="form.actTitle" placeholder="请输入标题" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('中介选择依据')" prop="mediationSelectionBasis">
            <el-input v-model="form.mediationSelectionBasis" type="textarea" :placeholder="$t('中介选择依据')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('立项后重点关注问题')" prop="issuesToFocusOnAfterProjectApproval">
            <el-input v-model="form.issuesToFocusOnAfterProjectApproval" type="textarea" :placeholder="$t('立项后重点关注问题')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
        <!--    拟招标中介机构    -->
        <formInnerTable
          title="拟招标中介机构"
          :data-list="form.sysYueyinDealIntermediarySelectionAgencies"
          :init-data="initData"
        >
          <el-table-column :label="$t('选聘类型')">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949; margin-right: 4px">*</i>
              <span>{{ $t("选聘类型") }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'sysYueyinDealIntermediarySelectionAgencies.' + scope.$index + '.selectionType'"
                :rules="rules.selectionType"
              >
                <dict-select
                  v-model="scope.row.selectionType"
                  dictionary="intermediary_selection_agency_selection_type"
                  :placeholder="$t('选聘类型')"
                />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :label="$t('中介机构名称')">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949; margin-right: 4px">*</i>
              <span>{{ $t("中介机构名称") }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'sysYueyinDealIntermediarySelectionAgencies.' + scope.$index + '.agencyId'"
                :rules="rules.agencyId"
              >
                <el-select v-model="scope.row.agencyId" :placeholder="$t('中介机构')">
                  <el-option
                    v-for="mediation in mediationList"
                    :key="mediation.id"
                    :label="$t(mediation.mediationName)"
                    :value="mediation.id"
                  />
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :label="$t('中介报价（万元）')">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949; margin-right: 4px">*</i>
              <span>{{ $t("中介报价（万元）") }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'sysYueyinDealIntermediarySelectionAgencies.' + scope.$index + '.intermediaryQuotation'"
                :rules="rules.intermediaryQuotation"
              >
                <Money v-model="scope.row.intermediaryQuotation" :placeholder="$t('中介报价（万元）')" unit="w" />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :label="$t('是否中标')">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949; margin-right: 4px">*</i>
              <span>{{ $t("是否中标") }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'sysYueyinDealIntermediarySelectionAgencies.' + scope.$index + '.whetherToWinTheBid'"
                :rules="rules.whetherToWinTheBid"
              >
                <dict-select
                  v-model="scope.row.whetherToWinTheBid"
                  dictionary="sys_yes_no"
                  :placeholder="$t('是否中标')"
                />
              </el-form-item>
            </template>
          </el-table-column>

        </formInnerTable>
      </el-row>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="intermediarySelection" />
    </el-form>
  </div>
</template>

<script>
import {
  getSysYueyinDealIntermediarySelection,
  saveSysYueyinDealIntermediarySelection
} from '@/api/sass/yueyin/deal/intermediarySelection/sysYueyinDealIntermediarySelection'

import { getDealName } from '@/api/base/deal/dealBase'
import formInnerTable from '@/views/saas/yueyin/deal/component/formInnerTable'
import { listMediationBase } from '@/api/deal/mediation/mediationBase'
export default {
  name: 'IntermediarySelection',
  components: {
    formInnerTable
  },
  data() {
    return {
      // 表单参数
      form: {
        sysYueyinDealIntermediarySelectionAgencies: [],
        fileList: undefined
      },
      // 表单校验
      rules: {
        actTitle: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        selectionType: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        agencyId: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        intermediaryQuotation: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        whetherToWinTheBid: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
      },
      initData: {
        selectionType: undefined,
        agencyId: undefined,
        intermediaryQuotation: undefined,
        whetherToWinTheBid: undefined
      },
      // 遮罩层
      loading: false,
      // 操作按钮默认不展示
      buttonShow: true,
      // 是否隐藏保存按钮
      otherBtnHide: false,
      mediationList: []
    }
  },
  created() {
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    async init(data) {
      console.log('asd', data)
      this.loading = true
      if (data != null && data.id !== undefined) {
        getSysYueyinDealIntermediarySelection(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        // 默认带出流程中必须要传的附件
        if (data !== null) {
          this.form.fileList = data.fileList
        }
        this.form.actTitle = await getDealName(this.$route.query.id) + '-中介机构选聘签署'
        this.loading = false
      }
      // 查询中介机构列表
      listMediationBase({}).then(result => {
        this.mediationList = result.rows
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: undefined,
        dealName: undefined,
        companyId: undefined,
        companyName: undefined,
        mediationSelectionBasis: undefined,
        issuesToFocusOnAfterProjectApproval: undefined,
        actId: undefined,
        actStatus: undefined,
        actTitle: undefined,
        remark: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        sysYueyinDealIntermediarySelectionAgencies: [],
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm(type) {
      // 关闭form页面遮罩层
      this.loading = false
      if (type !== 'actSave') {
        // 关闭form页面
        this.$parent.$parent.formOpen = false
        // 刷新list页面
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function(type) {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 保存数据
            saveSysYueyinDealIntermediarySelection(this.form).then(response => {
              console.log('type', type)
              console.log('保存结果', response)
              if (response.code === 200) {
                // 点击的按钮是保存并启动流程
                if (type === 'process') {
                  console.log('启动流程')
                  // 设置数据id方式启动流程后取消并再次启动会创建多条记录
                  this.form.id = response.data.id
                  // 启动流程
                  this.startProcess(response.data.id)
                  // 点击的仅保存
                } else {
                  console.log('仅保存')
                  this.msgSuccess(this.$t('保存成功'))
                  // 关闭form页面
                  this.closeForm(type)
                }
                this.form.id = response.data.id
                console.log('返回id', response.data.id)
                resolve(response.data.id)
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject(response)
              }
            }).catch(() => {
              this.loading = false
            })
          } else {
            reject()
            this.locationError(document)
          }
        })
      })
    },
    // 启动流程
    startProcess: async function(dataId) {
      // const dealName = await getDealName(this.$route.query.id)
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      processData.title = this.form.actTitle
      // 业务表名
      processData.dataTable = 'sys_yueyin_deal_intermediary_selection'
      console.log('业务id', dataId)
      // 业务ID
      processData.dataId = dataId
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/saas/yueyin/deal/intermediarySelection'
      processData.viewPath = '@/views/saas/yueyin/deal/intermediarySelection'
      // 菜单标识(根据菜单标识判断的走哪个流程)
      processData.menuKey = 'sys_yueyin_deal_intermediary_selection'
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    }
  }
}
</script>

<style scoped>

</style>
